/**
 * 封装wx.方法
 */

//模态框。确认、取消。有回调
const model = o => {
  wx.showModal({
    title: o.title || '提示',
    content: o.content || '',
    cancelText: o.cancelText || '取消',
    confirmText: o.confirmText || '确定',
    showCancel: o.showCancel == 'no' ? false : true,
    confirmColor: '#08be7c',
    success: (res) => {
      if (res.confirm) {
        o.success ? o.success() : ''
      } else if (res.cancel) {
        o.fail ? o.fail() : ''
      }
    }
  })
}


//页面跳转
const goPage = (url, type = 1, delta) => {
  if (type === 1) {
    wx.navigateTo({        // 保留当前页面，跳转到应用内的某个页面。但是不能跳到 tabbar 页面
      url: "/pages" + url
    })
  } else if (type === 2) { // 关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
    wx.redirectTo({
      url: "/pages" + url
    })
  } else if (type === 3) { // 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
    wx.switchTab({
      url: "/pages" + url,
    })
  }
  else if (type === 4) {   // 关闭当前页面，返回上一页面或多级页面。
    wx.navigateBack({
      delta: delta
    })
  } else if (type === 5) {
    wx.reLaunch({          // 关闭所有页面，打开到应用内的某个页面
      url: "/pages" + url
    })
  }
}

//获取手机型号
function getSystemInfo() {
  const res = wx.getSystemInfoSync()
  wx.setStorageSync('SYSTEM_INFO', res)
  return res
}

//输入地址转化为省市区
function conversionFn(str) {
  let area = {}
  let index11 = 0
  let index1 = str.indexOf("省")
  if (index1 == -1) {
    index11 = str.indexOf("自治区")
    if (index11 != -1) {
      area.Province = str.substring(0, index11 + 3)
    } else {
      area.Province = str.substring(0, 0)
    }
  } else {
    area.Province = str.substring(0, index1 + 1)
  }

  let index2 = str.indexOf("市")
  if (index11 == -1) {
    area.City = str.substring(index11 + 1, index2 + 1)
  } else {
    if (index11 == 0) {
      area.City = str.substring(index1 + 1, index2 + 1)
    } else {
      area.City = str.substring(index11 + 3, index2 + 1)
    }
  }

  let index3 = str.lastIndexOf("区")
  if (index3 == -1) {
    index3 = str.indexOf("县")
    area.Country = str.substring(index2 + 1, index3 + 1)
  } else {
    area.Country = str.substring(index2 + 1, index3 + 1)
  }
  return [area.Province || area.City || '', area.City || '', area.Country || ''];
}

//转天时分秒
function timeStamp(second_time) {
  var time = parseInt(second_time) + "秒钟";
  if (parseInt(second_time) > 60) {
    var second = parseInt(second_time) % 60;
    var min = parseInt(second_time / 60);
    time = min + "分钟";
    if (min > 60) {
      min = parseInt(second_time / 60) % 60;
      var hour = parseInt(parseInt(second_time / 60) / 60);
      time = hour + "小时" + min + "分钟";
      if (hour > 24) {
        hour = parseInt(parseInt(second_time / 60) / 60) % 24;
        var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
        time = day + "天" + hour + "小时" + min + "分钟";
      }
    }
  }
  return time;
}

const formatTime = date => {
  if (typeof date == 'string'){
    date = new Date(date.replace(/-/g, '/'));
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatTimeYMD = date => {
  if (typeof date == 'string'){
    date = new Date(date.replace(/-/g, '/'));
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-') 
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export default {
  model,
  goPage,
  getSystemInfo,
  conversionFn,
  timeStamp,
  formatTime,
  formatTimeYMD,
}